package com.group3.customer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.ha.backend.Sender;

import com.group3.customer.service.ICustomerMerchantService;
import com.group3.customer.service.IDishTypeService;
import com.group3.customer.service.MidListService;
import com.group3.customer.service.impl.CustomerMerchantServiceImpl;
import com.group3.customer.service.impl.DishTypeServiceImpl;
import com.group3.customer.service.impl.MidListServiceImpl;
import com.group3.customer.util.JsonUtil;
import com.group3.customer.util.SenderUtil;
import com.group3.entity.Customer;
import com.group3.entity.DishType;
import com.group3.entity.Merchant;

/**
 * Servlet implementation class MerchantListServlet
 */
public class MerchantListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MidListService midService = new MidListServiceImpl();
	private ICustomerMerchantService merchanService = new CustomerMerchantServiceImpl();
	private IDishTypeService dishTypeService = new DishTypeServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MerchantListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		merchantList(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		merchantList(request, response);
	}

	private void merchantList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<DishType> dishTypes = dishTypeService.getDishTypeList();
		System.out.println(dishTypes);
//		HttpSession session = request.getSession();
//		session.setAttribute("typeList", dishTypes);
		String type = request.getParameter("type");
		System.out.println("Type: " + type);
//		if(type == null) {
//			session.setAttribute("defaultType", "000");
//		} else {
//			session.setAttribute("defaultType", type);
//		}
		
		List<String> mIdList = midService.getMidList();
		System.out.println("midList: " + mIdList);
		List<Merchant> merchants = null;
		if(type == null || "000".equals(type) ) {
			merchants = merchanService.queryMerchants(mIdList);
		} else {
			merchants = merchanService.queryMerchantByDtId(mIdList,
					type);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("merchants", merchants);
		map.put("dishtype", dishTypes);
		HttpSession session=request.getSession();
		Customer user=(Customer) session.getAttribute("loginToken");
		map.put("loginToken", user);
		SenderUtil.send(map, response);
		
		
		
		
		
		
		
		
		
		
//		request.setAttribute("merchants", merchants);
//		request.getRequestDispatcher("listmerchants.jsp").forward(request,
//				response);
		
		
//		List<Merchant> merchants = new ArrayList<>();
//		merchants.add(new Merchant("001","west","1234","zhu","#","#"));
//		merchants.add(new Merchant("002","Louis","1234","ITA","#","#"));
//		merchants.add(new Merchant("003","Barry","1234","China","#","#"));
//		merchants.add(new Merchant("004","pauline","1234","tang","#","#"));
//		PrintWriter writer = response.getWriter();
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("merchants", merchants);
//		writer.write(JsonUtil.getInstance().ObjectToJson(map));
	}
}
